package com.cupster.base_super_resource;

import android.content.Context;
import android.content.res.ColorStateList;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.StateListDrawable;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.DrawableRes;
import androidx.core.content.ContextCompat;
import androidx.core.graphics.drawable.DrawableCompat;
import androidx.vectordrawable.graphics.drawable.VectorDrawableCompat;

public class SelectorHelper {

    //常用状态数组
    public static final int[] STATE_DEFAULT = new int[0];
    public static final int[] STATE_PRESSED = new int[]{android.R.attr.state_pressed, android.R.attr.state_enabled};
    public static final int[] STATE_SELECTED = new int[]{android.R.attr.state_selected};
    public static final int[] STATE_CHECKED = new int[]{android.R.attr.state_checked};
    public static final int[] STATE_UNCHECKED = new int[]{-android.R.attr.state_checked};
    public static final int[] STATE_DISABLED = new int[]{-android.R.attr.state_enabled};
    public static final int[] STATE_FOCUSED = new int[]{android.R.attr.state_focused};

    /**
     * 动态创建一个简单的选择器
     * @param context
     * @param view
     * @param resId
     */
    public static void createSelector(Context context, View view, @DrawableRes int... resId) {
        StateListDrawable stateListDrawable = new StateListDrawable();
        Drawable drawablePre = ContextCompat.getDrawable(context, resId[0]);
        Drawable drawableDef = ContextCompat.getDrawable(context, resId[1]);
        stateListDrawable.addState(STATE_PRESSED, drawablePre);
        stateListDrawable.addState(new int[0], drawableDef);
        stateListDrawable.setState(new int[0]);
        stateListDrawable.setEnterFadeDuration(200);
        stateListDrawable.setExitFadeDuration(200);
        view.setBackground(stateListDrawable);
    }

    /**
     * 通过对图片的着色创建选择器
     * @param context
     * @param view
     * @param isVector
     * @param resId
     */
    public static void createSelector(Context context, View view, boolean isVector, @DrawableRes int resId) {
        StateListDrawable stateListDrawable = new StateListDrawable();
        Drawable drawableSrc;
        if (isVector) {
            drawableSrc = VectorDrawableCompat.create(context.getResources(), resId, context.getTheme());
        } else {
            drawableSrc = ContextCompat.getDrawable(context, resId);
        }
        stateListDrawable.addState(STATE_PRESSED, drawableSrc);
        stateListDrawable.addState(new int[0], drawableSrc);
        stateListDrawable.setState(new int[0]);
        Drawable wrapDrawable = DrawableCompat.wrap(stateListDrawable);
        ColorStateList colorStateList = new ColorStateList(new int[][]{
                STATE_PRESSED,
                new int[0]
        }, new int[]{0XFFFF0000, 0xFF00FF00});
        DrawableCompat.setTintList(wrapDrawable, colorStateList);
        view.setBackground(stateListDrawable);
    }

    /**
     * 文字颜色选择器
     * @param v
     */
    public static void createSelector(TextView v) {
        ColorStateList colorStateList = new ColorStateList(new int[][]{
                STATE_PRESSED,
                new int[0]
        }, new int[]{Color.CYAN, Color.BLACK});
        v.setTextColor(colorStateList);
    }

    /**
     *
     * @param context        上下文
     * @param view        View
     * @param isVector 是否为矢量图
     * @param resId    图片数组
     */
    public static void createSelector(Context context, View view, boolean isVector, @DrawableRes int... resId) {
        StateListDrawable stateListDrawable = new StateListDrawable();
        Drawable drawableDis;
        Drawable drawablePre;
        Drawable drawableSel;
        Drawable drawableDef;
        if (isVector) {
            drawableDis = VectorDrawableCompat.create(context.getResources(), resId[0], context.getTheme());
            drawablePre = VectorDrawableCompat.create(context.getResources(), resId[1], context.getTheme());
            drawableSel = VectorDrawableCompat.create(context.getResources(), resId[2], context.getTheme());
            drawableDef = VectorDrawableCompat.create(context.getResources(), resId[3], context.getTheme());
        } else {
            drawableDis = ContextCompat.getDrawable(context, resId[0]);
            drawablePre = ContextCompat.getDrawable(context, resId[1]);
            drawableSel = ContextCompat.getDrawable(context, resId[2]);
            drawableDef = ContextCompat.getDrawable(context, resId[3]);
        }
        stateListDrawable.addState(STATE_DISABLED, drawableDis);
        stateListDrawable.addState(STATE_PRESSED, drawablePre);
        stateListDrawable.addState(STATE_SELECTED, drawableSel);
        stateListDrawable.addState(STATE_DEFAULT, drawableDef);
        stateListDrawable.setState(STATE_DEFAULT);
        stateListDrawable.setEnterFadeDuration(200);
        stateListDrawable.setExitFadeDuration(200);
        view.setBackground(stateListDrawable);
    }


}
